﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication9
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            
        }
       // Dictionary<string,int> sinhvienselected = new Dictionary<string,int>();
        private void button2_Click(object sender, EventArgs e)
        {
            button3.Text = "add";
            groupBox1.Visible = false;
            listBox1.Items.Clear();
            using (var context = new dbcontext())
            {
                context.sinhviens.Local.Count();
                
                foreach (sinhvien s in context.sinhviens)//lay ra mot phan tu
                {
                    listBox1.Items.Add(s.hoten.ToString());

                    //var h = context.sinhviens.Find(37);//tim kiem theo ma(masv)
                    //if (h != null)
                    //{
                    //    MessageBox.Show(h.hoten);
                    //}
                    //else
                    //{
                    //    MessageBox.Show("null");
                    //}
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            groupBox1.Visible = false;
            button4.Enabled = false;
            btxupdate.Enabled = false;
        }

        private void button3_Click(object sender, EventArgs e)
        {
           if (button3.Text=="add")
           {
               groupBox1.Visible = true;
               groupBox1.Text = "them sinh vien";
               button3.Text = "InSert";
           }
            if (button3.Text=="InSert")
            {
                thongbao.Text = "";
                
               try
               {
                   using (var context = new dbcontext())
                   {
                       sinhvien add_sinhvien = new sinhvien();
                       add_sinhvien.hoten = txthoten.Text;
                       add_sinhvien.tuoi = int.Parse(txttuoi.Text.ToString());
                       context.sinhviens.Add(add_sinhvien);
                       int row = context.SaveChanges();
                       if (row > 0)
                       {
                           thongbao.Text = "thanh cong";
                       }
                       //groupBox1.Visible = false;
                       txthoten.Text = null;
                       txttuoi.Text = null;
                   }
               }
               catch (System.Exception ex)
               {
                   thongbao.Text = "error";
               }
            }

        }
        public string sinhvienhoten_selected = "";
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedItems.Count>0)
            {
                button4.Enabled = true;
                sinhvienhoten_selected = listBox1.SelectedItem.ToString();
                groupBox1.Visible = false;
                btxupdate.Enabled = true;
                button3.Text = "add";
                try
                {
                    groupBox1.Visible = true;
                    groupBox1.Text = "cap nhat";
                    using (var context = new dbcontext())
                    {
                        var sinhvien_update = (from p in context.sinhviens where p.hoten == sinhvienhoten_selected select p);
                        foreach (sinhvien s in sinhvien_update)
                        {
                            txthoten.Text = s.hoten;
                            txttuoi.Text = s.tuoi.ToString();
                        }
                    }
                }
                catch (System.Exception ex)
                {

                }
            }
            else
            {
                button4.Enabled = false;
                btxupdate.Enabled = false;
                groupBox1.Visible = false;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            using (var context = new dbcontext())
            {
                var delete_sinhvien = from p in context.sinhviens where p.hoten == sinhvienhoten_selected select p;
                foreach (var sv in delete_sinhvien)
                {
                    context.sinhviens.Remove(sv);
                    //context.SaveChanges();
                    
                }
                listBox1.Items.RemoveAt(listBox1.SelectedIndex);
                 int i=context.SaveChanges();
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {

           try
           {
               using (var context = new dbcontext())
               {
                   var delete_sinhvien = from p in context.sinhviens where p.hoten == sinhvienhoten_selected select p;
                   foreach (var sv in delete_sinhvien)
                   {
                       sv.hoten = txthoten.Text;
                       sv.tuoi = int.Parse(txttuoi.Text.ToString());
                      
                       listBox1.Items.Insert(listBox1.SelectedIndex+1, txthoten.Text);
                       listBox1.Items.RemoveAt(listBox1.SelectedIndex);
                   }
                   context.SaveChanges();

               }
           }
           catch (System.Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
        }
    }
}
